//
//  ViewController.m
//  TestAfn2
//
//  Created by xujingjing on 16/6/15.
//  Copyright © 2016年 xujingjing. All rights reserved.
/*
 
 */

#import "ViewController.h"
#import "MJNewsListModel.h"
#import "MJhealthModel.h"
#import "PingJIACELL.h"
#import "MJNewsListViewModel.h"

#import "MoreVC.h"
#import "NewAlbunVC.h"
#import "HMSegmentedControl.h"
#import "CountdownView.h"

#import "Constants.h"//侧滑宏定义
#import "SZKCleanCache.h"  //缓存

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>


@property (strong,nonatomic) MJNewsListViewModel *newsViewModel;


@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIButton *textLable;
@property (weak, nonatomic) IBOutlet UIView *headerView;

@property (strong, nonatomic)  HMSegmentedControl *HMSegment;
@property (weak, nonatomic) IBOutlet CountdownView *countDownView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(getNetworkState:) name:@"NETWORKSTATE" object:nil];

    [self.newsViewModel addObserver:self forKeyPath:[self.newsViewModel observerKeyPath] options:NSKeyValueObservingOptionNew context:nil];

#pragma mark－倒计时
    _countDownView.frame = CGRectMake(0, 0, 100, 20);
    _countDownView.timeType = 0;
    _countDownView.backColor = [UIColor whiteColor];
    _countDownView.textColor = [UIColor blackColor];
    _countDownView.pointColor = [UIColor blackColor];
    _countDownView.backgroundColor = [UIColor clearColor];
    
    NSString * Time= @"05:23:10";
    [_countDownView countdownViewSetStartTime:Time];
    
    //输出缓存大小 m
    NSLog(@"缓存大小%.2fm",[SZKCleanCache folderSizeAtPath]);
    
    //    //清楚缓存
    //    [SZKCleanCache cleanCache:^{
    //        NSLog(@"清除成功");
    //    }];
    //    

    
    
}
- (IBAction)leftSlide:(id)sender {
    
    [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationLeftSlide object:nil];

    
}


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
   
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        [_tableView reloadData];

    });
    

}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{


    NSArray *titlesTop = @[@"图文详情",@"产品评论"];
    self.HMSegment = [[HMSegmentedControl alloc]initWithSectionTitles:titlesTop];
    self.HMSegment.font = FONT(15);
    self.HMSegment.backgroundColor = Color_clear;
    self.HMSegment.selectionIndicatorHeight = 1;
    self.HMSegment.selectionIndicatorColor = [UIColor redColor];
    self.HMSegment.selectedTextColor = [UIColor redColor];
    self.HMSegment.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
    self.HMSegment.selectionStyle = HMSegmentedControlSelectionStyleFullWidthStripe;
    self.HMSegment.frame = CGRectMake(0, 0, SCREEN_WIDTH, 39);
    self.HMSegment.selectedSegmentIndex = 0;
    //    self.HMSegment.isTouchAgain = YES;
    [self.HMSegment addTarget:self action:@selector(selectedSourceType:) forControlEvents:UIControlEventValueChanged];

    [_headerView addSubview:self.HMSegment];
    [_headerView addSubview:_countDownView];
    return _headerView;

}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 80;
}
-(void)selectedSourceType:(HMSegmentedControl *)segment{
    {
      
        switch (self.HMSegment.selectedSegmentIndex) {
            case 0:
            {
                self.HMSegment.backgroundColor = ColorWithOne(300);
                NSLog(@"%ld",self.HMSegment.selectedSegmentIndex);
                //return;
            }
                
                break;
            case 1:
            {
                
                self.HMSegment.backgroundColor = ColorWithOne(244);
                NSLog(@"%ld",self.HMSegment.selectedSegmentIndex);

                //return;
                break;
            }
   
            default:
                break;
        }
        
    }

}

- (void)getNetworkState:(NSNotification *)notification{

    NSLog(@"%@",notification.userInfo);
    
    [_textLable setTitle:[NSString stringWithFormat:@"当前网络:%@",notification.userInfo[@"netState"]] forState:UIControlStateNormal];
    [_textLable setBackgroundImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://teehanlax.com.s3.amazonaws.com/wordpress/wp-content/uploads/mvvm1.png"]]] forState:UIControlStateNormal];
}

//- (void)mjModelTest:(NSDictionary *)response{
//   
//    _mjnewsModel = [MJNewsListModel mj_objectWithKeyValues:response];
//    
//    _mjHealthModel = [MJhealthModel mj_objectWithKeyValues:_mjnewsModel.newslist[0]];
//    
////    NSLog(@"newsModel:%@,code=%d",newsModel.newslist,newsModel.code);
////    NSLog(@"mjHealthModel,picUrl=%@,title=%@,url=%@",_mjHealthModel.picUrl,_mjHealthModel.title,_mjHealthModel.url);
// 
//
//
//}


#pragma markTableVIewDelegate
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
   static NSString *cellId = @"cell";
    PingJIACELL *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[PingJIACELL alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
        
    }

    // cell.healthModel = [MJhealthModel mj_objectWithKeyValues:_newsViewModel.model.newslist[indexPath.row]];
      cell.healthModel = self.newsViewModel.model.dataSource[indexPath.row];
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return  self.newsViewModel.model.dataSource.count;
 //   return self.newsViewModel.model.newslist.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 100;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

- (void)dealloc {
    [self.newsViewModel removeObserver:self forKeyPath:[self.newsViewModel observerKeyPath]];
}
#pragma markgetter and setter

- (MJNewsListViewModel *)newsViewModel{
    if (!_newsViewModel) {
        _newsViewModel = [[MJNewsListViewModel alloc]init];
        
        [_newsViewModel pullDown];
    }
    return _newsViewModel;
}



@end
